import './Array.extend'
import * as Api from "@/service";

/* eslint-disable */

function toString(obj) {
    return Object.prototype.toString.call(obj);
}

function isType(type) {
    return function(target) {
        return toString(target) === `[object ${type}]`
    }
}

const isString = isType('String');
const isObject = isType('Object');
const isArray = isType('Array');
const isBoolean = isType('Boolean');
const isEmptyObj = function(obj) {
    return !Object.keys(obj).length;
}
const isWx = function() {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.indexOf('micromessenger') < 0) {
        return false;
    }
    return true;
}

const addQuery = function(val) {
    let hash = location.hash
    if (hash.indexOf('?') > -1)
        location.hash += `&${val}`
    else
        location.hash += `?${val}`
}

const isPhone = function() {
    return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)
}

export default {
    scrollResolve: function() {
        let currentPosition, timer;
        let speed = 1;
        timer = setInterval(function() {
            currentPosition = document.documentElement.scrollTop || document.body.scrollTop;
            currentPosition -= speed;
            window.scrollTo(0, currentPosition); //页面向上滚动
            currentPosition += speed;
            window.scrollTo(0, currentPosition); //页面向下滚动
            clearInterval(timer);
        }, 100);
    },
    queryString: function() {
        return window.location.search.substring(1).split('&').reduce((p, next) => {
            const [k, v] = next.split('=')
            p[k] = v;
            return p;
        }, {})
    },
    getChannelInfo: function() {
        let channel = this.queryString().channel ? this.queryString().channel.toLowerCase() : 'other'
        let value = ''
            // ds渠道
        if (channel.indexOf('_') && channel.indexOf('ds') > -1) {
            value = channel.split('_')[1]
            return {
                channel,
                value: value,
                type: 'ds'
            }
        }
        // app | other 渠道
        const CHANNELS = ['meitu', 'wuta1', 'wuta2', 'b612', 'music', 'mangotv', 'official_wechat', 'official_weibo']
        if (CHANNELS.indexOf(channel) > -1) {
            if (channel.indexOf('official') > -1) {
                return {
                    channel,
                    type: 'official'
                }
            }
            return { channel, type: 'app' }
        } else if (channel === 'other' || channel === '' || channel === undefined) {
            return {
                channel: 'other',
                type: 'app'
            }
        }
        return 'channel err'
    },
    isOver: async function(disc) {
        let channel_info = this.getChannelInfo()
        console.log(channel_info)
        let overGetTime = ''
        const discern = localStorage.getItem('camellia-sample-discern') || disc
        console.log(discern)
        let type = ''
        if (disc) {
            const { data } = await Api.getCheckInfo({
                discern
            });
            console.log(data)
            type = data.type
        }
        if (type === 'ds') {
            overGetTime = new Date(2019, 8, 26, 23, 59, 59).getTime();
        } else if (
            channel_info.type === 'ds'
        ) {
            if (discern) {
                overGetTime = new Date(2019, 8, 26, 23, 59, 59).getTime();
                console.log(overGetTime)
            } else {
                overGetTime = new Date(2019, 8, 19, 23, 59, 59).getTime();
            }
        } else {
            if (discern) {
                overGetTime = new Date(2019, 8, 16, 23, 59, 59).getTime();
            } else {
                overGetTime = new Date(2019, 8, 15, 23, 59, 59).getTime();
            }
        }

        const now = Date.now()
        console.log(now > overGetTime)
        if (now > overGetTime) {
            return true
        }
        return false
    },
    isEmptyObj,
    isString,
    isObject,
    isBoolean,
    isArray,
    isWx,
    isPhone,
    addQuery
}